import { Controller } from "../../mvc/core/Controller";
import { EBattlefieldPlayerState } from "../model/defin/EBattlefieldPlayerState";
import { IView } from "../../mvc/interfaces/IView";
import { BattlefieldModule } from "../../../modules/BattlefieldModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SBattlefieldOpen } from "../../../Message/Public/SBattlefieldOpen";
import { ClockManager } from "../manager/ClockManager";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { DataEvent } from "../events/DataEvent";
import { SystemSetter } from "../view/system/SystemSetter";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { GameProxy } from "../mvc/GameProxy";
type int = number;
//class BattlefieldController
    
    export  class BattlefieldController extends Controller
    {
        constructor()
        {
            
            super();this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._battlefieldModule) 
            {
                this._battlefieldModule = new BattlefieldModule();
            }
            return this._battlefieldModule;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldOpen, this.onBattlefieldOpen);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldClose, this.onBattlefieldClose);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldListUpdate, this.onBattlefieldList);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldPlayerScoresUpdate, this.onBattlefieldPlayerScoresUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldPlayerCreditUpdate, this.onBattlefieldPlayerCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldCampCreditUpdate, this.onBattlefieldCampCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldEnter, this.onBattlefieldEnter);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldLeave, this.onBattlefieldLeave);
            NetDispatcher.addCmdListener(ServerCommand.FightModelCampUnionUpdateCommand, this.onCampUnionUpdate);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddBattleField, this.onActiveBattleFieldHandler);
            Dispatcher.addEventListener(EventName.BattlefieldShowIntroduce, this.onBattlefieldShowIntroduce);
            Dispatcher.addEventListener(EventName.BattlefieldRequestList, this.onBattlefieldRequestList);
            Dispatcher.addEventListener(EventName.BattlefieldRequestComein, this.onBattlefieldRequestComein);
            Dispatcher.addEventListener(EventName.BattlefieldRequestResult, this.onBattlefieldRequestResult);
            Dispatcher.addEventListener(EventName.BattlefieldRequestLeaveAffirm, this.onBattlefieldRequestLeaveAffirm);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onSettingChange);
            return;
        }

        /* internal  */onBattlefieldOpen(arg1: SBattlefieldOpen): void
        {
            this._isCloseBattleField = false;
            this._battlefieldOpen = arg1;
            this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
            this._isAtStartTime = ClockManager.instance.nowDate.time - this._battlefieldOpen.openDt.time < 5000;
            var loc1=ClockManager.instance.nowDate.time - this._battlefieldOpen.openDt.time < -2000;
            this.updateViewState();
            if (!this._isAtStartTime && cache.role.entityInfo.level >= LevelLimitDefin.BattlefieldMinLevel) 
            {
                (view as BattlefieldModule).showEntrance();
            }
            if (!loc1 && cache.role.entityInfo.level >= LevelLimitDefin.BattlefieldMinLevel) 
            {
                (view as BattlefieldModule).showLightMask();
            }
            else 
            {
                (view as BattlefieldModule).hideLightMask();
            }
            if (loc1) 
            {
                (view as BattlefieldModule).startCountdownLimitEnter(this._battlefieldOpen.openDt);
            }
            return;
        }

        /* internal  */onBattlefieldClose(arg1: Object): void
        {
            this._isCloseBattleField = true;
            if (!this._isInBattlefield) 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
                this.updateViewState();
            }
            (view as BattlefieldModule).setCanLeave();
            return;
        }

        /* internal  */onBattlefieldEnter(arg1: Object): void
        {
            this._isInBattlefield = true;
            this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusOpen;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
            this.updateViewState();
            this.battlefieldCountDownUpdate(this._battlefieldOpen.endDt);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            this.lastSettingHideLifeMagic = SystemSetter.currentSetter.hideLifeMagic;
            SystemSetter.currentSetter.hideLifeMagic = false;
            ThingUtil.entityUtil.updateLifeShow();
            return;
        }

        /* internal  */onBattlefieldLeave(arg1: Object): void
        {
            this._isInBattlefield = false;
            if (this._isCloseBattleField) 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
            }
            else 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
            this.updateViewState();
            if (this.lastSettingHideLifeMagic && !this.isChangeAtMiddle) 
            {
                SystemSetter.currentSetter.hideLifeMagic = true;
                ThingUtil.entityUtil.updateLifeShow();
            }
            return;
        }

        /* internal  */onCampUnionUpdate(arg1: SAttribute): void
        {
            (view as BattlefieldModule).updateUnionState(arg1);
            return;
        }

        /* internal  */onMoneyUpdate(arg1: Object): void
        {
            (view as BattlefieldModule).updateHonor(cache.role.money.honour);
            return;
        }

        /* internal  */onSettingChange(arg1: DataEvent): void
        {
            this.isChangeAtMiddle = true;
            return;
        }

        /* internal  */onActiveBattleFieldHandler(arg1: SActiveToClient): void
        {
            if (this._battlefieldModule) 
            {
                this._battlefieldModule.updateActiveReward(arg1);
            }
            return;
        }

        /* internal  */battlefieldCountDownUpdate(arg1: Date): void
        {
            (view as BattlefieldModule).startCountdown(arg1);
            return;
        }

        /* internal  */onBattlefieldList(arg1: Array<any>): void
        {
            (view as BattlefieldModule).updateBattlefieldList(arg1);
            return;
        }

        /* internal  */onBattlefieldPlayerScoresUpdate(arg1: Object): void
        {
            (view as BattlefieldModule).updatePlayerScoreList(cache.battlefield.vccampPlayerScoreAll);
            return;
        }

        /* internal  */onBattlefieldPlayerCreditUpdate(arg1: Object): void
        {
            (view as BattlefieldModule).updateMyScore(cache.battlefield.myScore);
            return;
        }

        /* internal  */onBattlefieldCampCreditUpdate(arg1: Object): void
        {
            (view as BattlefieldModule).updateCampScore(cache.battlefield.campScore1, cache.battlefield.campScore2, cache.battlefield.campScore3);
            return;
        }

        /* internal  */onBattlefieldShowIntroduce(arg1: DataEvent): void
        {
            (view as BattlefieldModule).showEntrance();
            return;
        }

        /* internal  */onBattlefieldRequestList(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.getBattleFieldInfo();
            return;
        }

        /* internal  */onBattlefieldRequestComein(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.enterBattleField(arg1.data as int);
            return;
        }

        /* internal  */onBattlefieldRequestResult(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.getBattleFieldScoreList();
            return;
        }

        /* internal  */onBattlefieldRequestLeaveAffirm(arg1: DataEvent): void
        {
            GameProxy.battlefieldProxy.leftBattleField();
            return;
        }

        /* internal  */updateViewState(): void
        {
            (view as BattlefieldModule).updateState(this._currentBFState);
            return;
        }

        /* internal  */isInBattlefieldTime(): Boolean
        {
            if (!this._battlefieldOpen) 
            {
                return false;
            }
            if (ClockManager.instance.nowDate.time > this._battlefieldOpen.openDt.time && ClockManager.instance.nowDate.time < this._battlefieldOpen.endDt.time) 
            {
                return true;
            }
            return false;
        }

        private /* var */_battlefieldModule: BattlefieldModule;

        private /* var */_currentBFState: EBattlefieldPlayerState;

        private /* var */_battlefieldOpen: SBattlefieldOpen;

        private /* var */_isAtStartTime: Boolean=false;

        private /* var */_isInBattlefield: Boolean=false;

        private /* var */lastSettingHideLifeMagic: Boolean=false;

        private /* var */isChangeAtMiddle: Boolean=false;

        private /* var */_isCloseBattleField: Boolean=true;
    }
